package forum;

import java.util.ArrayList;
import java.util.List;

import database.DBHandler;


public class UsersHandler {
	private ArrayList<String> loggedIns;
	private boolean isWeb;
	private DBHandler dbHandler;

	public UsersHandler (DBHandler dbHandler){
		this.loggedIns = new ArrayList<String>();
		this.isWeb = false;
		this.dbHandler = dbHandler;
	}

	/**
	 * @return the isWeb
	 */
	public boolean isWeb() {
		return isWeb;
	}

	/**
	 * @param isWeb the isWeb to set
	 */
	public void setWeb(boolean isWeb) {
		this.isWeb = isWeb;
	}

	/**
	 * This method adds new user to the list of registered users.
	 * @param newUser: the user to add
	 * @return true if the adding was successful and false otherwise
	 * !!!!!!!!!!!!!!!!!!!!!!!!!Important!!! Will be Useful For REGISTRATION!!!!!!!!!!!!!!!!!
	 */
	public synchronized boolean registerUser (Member newUser, String password){
		int res = dbHandler.registerUser(newUser.getUsername(), 
				newUser.getFirstName(), newUser.getLastName(), 
				password, newUser.getEMail(), newUser.getGender());
		
		if(res == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * This method removes the user with given username and also removes him from
	 * 								other users' friends' lists
	 * @param username: the username of the user to be removed
	 * @return username of removed user if the removing was successful and false otherwise
	 * !!!!!!!!!!!!!!!!Important!!! assuming that the check if the other user has the
	 * 								authority to remove this user was done in STORAGE!!!!!!!
	 */
	public synchronized int removeUser (String username){
		int ans = 1;

		ans = dbHandler.removeUser(username);
		if (ans == 1) {
			return 0; 
		}
		return -1;
	}

	/**
	 * @param username: the name of the user to find.
	 * @return the index of theuser in the list if user with given username exists
	 *	       and -1 otherwise.
	 */
	public synchronized int findUser(String username) {
		/*if(isWeb) {
			return 1;
		}*/
		for (int i = 0; i < this.loggedIns.size(); i++)
			if (this.loggedIns.get(i).equals(username))
				return i;
		return -1;
	}

	/**
	 * This method adds a specified friend to specified user's list of friends
	 * @param username: the user who wants to add the friend
	 * @param friendName: the name of the friend to add
	 * @return true if the adding was successful
	 *         and false if the adding has failed.
	 */
	public synchronized boolean addUserFriend(String username, String friendName){
		int res =  dbHandler.addFriend(username, friendName);
		if(res == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * This method removes a specified friend from specified user's list
	 * @param username: the user who wants to remove friend.
	 * @param friendName: the name of the friend to remove
	 * @return the name of the removed friend if both user and friend exist in the list
	 * 			and empty string if one of them does not exist in the list or the removing
	 * 			has failed.
	 */
	public synchronized boolean removeUserFriend(String username, String friendName){
		int res =  dbHandler.removeFriend(username, friendName);
		if(res == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * This method changes the status of the user who wishes to log in to logged in.
	 * @param username: the name of the user to log in
	 * @return true if the user exists in the list and he is logged in successfully
	 *         and false otherwise.
	 */
	public synchronized boolean login(String username, String password){
		int result = dbHandler.login(username, password);
		if (result == 0) {
			return false;
		}
		this.loggedIns.add(username);
		return true;
	}

	/**
	 * This method changes the status of the user who wishes to log in to logged in.
	 * @param username: the name of the user to log in
	 * @return true if the user exists in the list and he is logged off successfully
	 *         and false otherwise.
	 */
	public synchronized boolean logoff(String username){
		int index = findUser(username);
		if (index != -1){
			this.loggedIns.remove(index);
			return true;
		}
		return false;
	}

	/**
	 * This method returns the list of registered users' usernames.
	 * @return the list of registered users' usernames.
	 * !!!!!!!!!!!!!!!!!!!Important!!! Don't return the list such it is because we don't want
	 * 						anyone outside of this class to be able to see and maybe even
	 * 						change users' data.
	 */
	public synchronized List<String> getUsers(){
		List<String> namesOfRegisteredUsers = new ArrayList<String>();
		/*
		for (int i = 0; i < this.loggedIns.size(); i++){
			namesOfRegisteredUsers.add(this.loggedIns.get(i).getUsername());
		}
		*/
		return namesOfRegisteredUsers;
	}

	////////////////////////////////////////TO DELETE AFTER TESTS!!!!///////////////////
	
	public synchronized ArrayList<String> getTotalUsers(){
		return this.loggedIns;
	}

	/**
	 * @return the number of registered users.
	 */
	public int numOfUsers(){
		return this.loggedIns.size();
	}


	/**
	 * @param username: the name of the user to check.
	 * @return true if thie user exists and he is a moderator
	 * 			and false if he does not exist or he is not a moderator.
	 */
	public synchronized boolean isModerator(String username){
		return dbHandler.isModerator(username);
	}

	public synchronized boolean setModerator(String username, String userToSet){
		/*
		int index1 = findUser(username);
		int index2 = findUser(userToSet);
		if (index1 != -1 && index2 != -1){
			if (isModerator(username)){
				this.loggedIns.get(index2).setModeratorRole();
				return true;
			}
		}
		*/
		return false;
	}

	public synchronized boolean isUserLoggedin(String username){
		/*if(isWeb) {
			return true;
		}*/
		return this.loggedIns.contains(username);
	}

    public synchronized List<Member> getListOfFriends(String username){
    	return dbHandler.getFriends(username);
    }
    
    public Member getInfoAboutFriend(String friendName) {
		return dbHandler.getInfoAboutFriend(friendName);
	}
    
    public List<Post> getThread(int threadID) {
		return dbHandler.getThread(threadID);
	}

}
